var status;
var p;
var pointsId = 167200;
var winPoint = 0;//任務全部通過獎勵的段位點。

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == 0) {
        cm.dispose();
        return;
    } else if (mode == 1) {
        status++;
    } else {
        status--;
    }

    if (status == 0) {
        var eim = cm.getEventInstance();
        p = new Points();
        var s = state(eim.getProperty("state"));
        if (s == 0 || s == 999) {
            winPoint = 0;
            cm.sendYesNo("你真的要離開這裡嗎?離開後就沒有獎勵了。");
        } else {
            winPoint = 1;
            status = 1;
            cm.sendYesNo("任務還沒有真正完成，只能領取到一部分獎勵，確定嗎？");
        }
    } else if (status == 1) {
        cm.warp(940020000, 0);
        cm.dispose();
    } else if (status == 2) {
        p.setEventPoints(pointsId, cm.getPlayer().getId(), winPoint);
        var points = p.getEventPoints(pointsId, cm.getPlayer().getId());//目前積分
        var Ranking = RankingName(points);
        cm.warp(940020000, 0);
        cm.sendOk("獲取了" + winPoint + "段位點。\r\n目前情況：\r\n段位：" + Ranking + "（" + points + "）分。\r\n完成任務獲取點數+" + winPoint + "。");
        cm.dispose();
    }
}

function RankingName(points) {
    if (points >= 0 && points <= 30) {
        return "青銅";
    }
    if (points >= 31 && points <= 50) {
        return "白銀";
    }
    if (points >= 51 && points <= 80) {
        return "黃金";
    }
    if (points >= 81 && points <= 100) {
        return "白金";
    }
    if (points >= 101 && points <= 120) {
        return "鑽石";
    }
    if (points >= 121 && points <= 180) {
        return "超凡大師";
    }
    if (points > 180) {
        return "最強王者";
    }
}



var Points = function() {
    this.DelEventPoints = function(Eventid, charid) {
        var delectData = cm.getConnection().prepareStatement("delete from EventTimes where eventid = " + Eventid + " and cid = " + charid + "");
        delectData.executeUpdate(); //删除数据
    }

    this.DelEventPoints = function(Eventid, charid) {
        var delectData = cm.getConnection().prepareStatement("delete from EventTimes where eventid = " + Eventid + " and cid = " + charid + "");
        delectData.executeUpdate(); //删除数据
    }

    this.getEventTimes = function(Eventid, charid) {//通过eventid来得到参与这个活动的次数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i = Times.getString("times");//得到次数
        }
        return parseInt(i);
    }

    this.getEventPoints = function(Eventid, charid) {//通过eventid来得到参与这个活动的点数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i = Times.getString("points");//得到点数
        }
        return parseInt(i);
    }

    this.setEventPoints = function(Eventid, charid, points) {//通过eventid来给予参与这个活动的点数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i++;
        }
        if (i == 0) {//insert
            var insert = cm.getConnection().prepareStatement("INSERT INTO EventTimes VALUES(?,?,?,?,?,?,?)"); // 载入数据
            insert.setString(1, null); //载入记录ID
            insert.setString(2, Eventid); //载入活动ID
            insert.setString(3, cm.getPlayer().getId());//cid
            insert.setString(4, cm.getPlayer().getName());//cname
            insert.setString(5, points);//points 点数
            insert.setString(6, this.getEventTimes(1, charid));//times 次数
            insert.setString(7, null);//
            insert.executeUpdate(); //更新
        } else {//update
            var update = cm.getConnection().prepareStatement("update EventTimes set points = ? where eventid = " + Eventid + " and cid = " + charid + "");//更新为已使用
            update.setString(1, this.getEventPoints(Eventid, charid) + points);
            update.executeUpdate();
        }
    }

    this.setEventTimes = function(Eventid, charid, times) {//通过eventid来设置参与这个活动的次数
        var i = 0;
        var Times = cm.getConnection().prepareStatement("SELECT * FROM EventTimes where eventid = " + Eventid + " and cid = " + charid + "").executeQuery(); // 查询数据
        while (Times.next()) {
            i++;
        }
        if (i == 0) {//insert
            var insert = cm.getConnection().prepareStatement("INSERT INTO EventTimes VALUES(?,?,?,?,?,?,?)"); // 载入数据
            insert.setString(1, null); //载入记录ID
            insert.setString(2, Eventid); //载入活动ID
            insert.setString(3, cm.getPlayer().getId());//cid
            insert.setString(4, cm.getPlayer().getName());//cname
            insert.setString(5, this.getEventPoints(2, charid));//points 点数
            insert.setString(6, times);//times 次数
            insert.setString(7, null);//
            insert.executeUpdate(); //更新
        } else {//update
            var update = cm.getConnection().prepareStatement("update EventTimes set times = ? where eventid = " + Eventid + " and cid = " + charid + "");//更新为已使用
            update.setString(1, this.getEventTimes(Eventid, charid) + times);
            update.executeUpdate();
        }
    }
}


function state(pro) {
    switch (pro) {
        case "2":
        case "5":
        case "8":
        case "11":
            return 0;
            break;
        case "3":
        case "6":
        case "9":
        case "12":
            return 0;
            break;
        case "4":
            return 1;//證明過了一個關卡了
        case "7":
            return 2;//證明過了一個關卡了
        case "10":
            return 3;//證明過了一個關卡了
            break;
        case "13"://任務完成部分
            return 999;
            break;
    }
}